﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.App;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.DALFactory;
namespace HKEA.CMS.Core.DAL.App
{
    public class app_tblBrand_ProdCatDAL:Iapp_tblBrand_ProdCat
    {
        HKEA_CMSEntities model;
        app_tblBrand_ProdCat brand_procat;
        private app_tblBrand_ProdCatDAL()
        {
            model = DataAccess.GetModelInstance();
        }

        public static app_tblBrand_ProdCatDAL GetInstance()
        {
            return new app_tblBrand_ProdCatDAL();
        }

        public int InsertENT(app_tblBrand_ProdCat ent)
        {
            ent.CreationDate = DateTime.Now;
            ent.LastModifyDate = DateTime.Now;
            model.app_tblBrand_ProdCat.AddObject(ent);
            if (model.SaveChanges() > 0)
                return model.app_tblBrand_ProdCat.Max(a => a.AppBrandProdCatUID);
            return 0;
        }
        public int InsertList(List<app_tblBrand_ProdCat> list)
        {
            foreach (var ent in list)
            {
                ent.CreationDate = DateTime.Now;
                ent.LastModifyDate = DateTime.Now;
                model.app_tblBrand_ProdCat.AddObject(ent);
            }
            return model.SaveChanges();
        }

        public List<app_tblBrand_ProdCat> GetListByENT(app_tblBrand_ProdCat ent, bool AddDeletedForSelect)
        {
            var ret = model.app_tblBrand_ProdCat.Where(a => a.AppBrandProdCatUID > 0);
            if (AddDeletedForSelect)
                ret = ret.Where(a => a.Deleted == ent.Deleted);
            if (ent.AppBrandUID > 0)
                ret = ret.Where(a => a.AppBrandUID == ent.AppBrandUID);
            if (ent.BrandUID > 0)
                ret = ret.Where(a => a.BrandUID == ent.BrandUID);
            return ret.ToList();
        }

        public int GetCountByENT(app_tblBrand_ProdCat ent)
        {
            throw new NotImplementedException();
        }

        public int UpdateENT(app_tblBrand_ProdCat ent)
        {
            var ret = model.app_tblBrand_ProdCat.Where(a => a.AppBrandProdCatUID == ent.AppBrandProdCatUID).FirstOrDefault();
            if (ret != null)
            {
                ret.AppBrandUID=ent.AppBrandUID;
                ret.BrandProdCatUID=ent.BrandProdCatUID;
                ret.BrandUID=ent.BrandUID;
                ret.ProductCatUID=ent.ProductCatUID;
                ret.Deleted = ent.Deleted;
                ret.LastModifyDate = DateTime.Now;
            }
            return model.SaveChanges();
        }

        public int DeleteENT(app_tblBrand_ProdCat ent)
        {
            var ret = model.app_tblBrand_ProdCat.Where(a => a.AppBrandProdCatUID == ent.AppBrandProdCatUID).FirstOrDefault();
            if (ret != null)
            {
                model.app_tblBrand_ProdCat.DeleteObject(ret);
            }
            return model.SaveChanges();
        }


        public int DeleteByKeyList(List<int> list)
        {
            foreach (var temp in list)
            {
                var result = model.app_tblBrand_ProdCat.Where(a => a.AppBrandProdCatUID == temp).FirstOrDefault();
                if (result != null)
                    model.app_tblBrand_ProdCat.DeleteObject(result);
            }
            return model.SaveChanges();
        }

        public int SaveList(List<int> ToDeletedKey, List<app_tblBrand_ProdCat> AddList, List<app_tblBrand_ProdCat> AddToDeletedList)
        {
            foreach (var temp in ToDeletedKey)
            {
                var result = model.app_tblBrand_ProdCat.Where(a => a.AppBrandProdCatUID == temp).FirstOrDefault();
                if (result != null)
                    model.app_tblBrand_ProdCat.DeleteObject(result);
            }
            foreach (var temp in AddList)
            {
                temp.CreationDate = DateTime.Now;
                temp.LastModifyDate = DateTime.Now;
                model.app_tblBrand_ProdCat.AddObject(temp);
            }
            foreach (var temp in AddToDeletedList)
            {
                temp.CreationDate = DateTime.Now;
                temp.LastModifyDate = DateTime.Now;
                model.app_tblBrand_ProdCat.AddObject(temp);
            }
            return model.SaveChanges();
        }
    }
}
